#!/bin/bash

DIR=$(dirname "$0")
source ${DIR}/avd.inc

set -e #exit on error.

PIDFILE=`mktemp -t androidtests.XXXXXXX`
EMULATOR_BIN=`which emulator`

for api in ${APIS}; do
    avd="klaxon-${api}"
    echo "starting avd: ${avd}"
    start-stop-daemon -p ${PIDFILE} -S --exec ${EMULATOR_BIN} -m -b -- \
      -avd ${avd} -sdcard ${SDCARD} -no-boot-anim

    adb wait-for-device
    sleep 30
    echo "installing...."
    ant debug install >/dev/null
    cd tests && ant debug install >/dev/null
    cd ..
    adb shell am instrument -w org.nerdcircus.android.klaxon.tests/android.test.InstrumentationTestRunner

    #spin down the emulator.
    start-stop-daemon -K -p ${PIDFILE}
    rm ${PIDFILE}
    sleep 5
done
